import axios from 'axios';
import base from './config';
// const baseURL = 'http://localhost:8080';
const baseURL= `${base}`

export const codeValidateService = async (code) => { 
    try {
        const response = await axios.get(`${baseURL}/api/invitationCode/validate/${code}`);
        return response.data;
    } catch (error) {
        console.error('Error validating code:', error);
        throw error;
    }
}

export const checkUsernameService = async (username) => {
    try {
        const response = await axios.get(`${baseURL}/api/user/check-username/${username}`);
        return response.data;
    } catch (error) {
        console.error('Error checking username:', error);
        throw error;
    }
}

export const RegisterService = async (userData) => {
    try {
        const response = await axios.post(`${baseURL}/api/user/register`, userData);
        return response.data;
    } catch (error) {
        console.error('Error during registration:', error);
        throw error;
    }
};

export const LoginService = async (userData) => {
    try {
        const response = await axios.post(`${baseURL}/api/user/login`, userData);
        return response.data;
    } catch (error) {
        console.error('Error during login:', error);
        throw error;
    }
}

export const GetUserInfoService = async () => {
    try {
        const response = await axios.get(`${baseURL}/api/user/info`);
        return response.data;
    } catch (error) {
        console.error('Error fetching user info:', error);
        throw error;
    }
};

export const fetchUserExpService = async () => {
    try {
        const response = await axios.get(`${baseURL}/api/user/exp`);
        return response.data;
    } catch (error) {
        console.error('Error fetching user experience:', error);
        throw error;
    }
};

export const updatePasswordService = async (userId, passwordData) => {
    try {
        const response = await axios.put(`${baseURL}/api/user/${userId}`, passwordData);
        return response.data;
    } catch (error) {
        console.error('Error updating password:', error);
        throw error;
    }
};

export const fetchCodesService = async () => {
    try {
        const response = await axios.get(`${baseURL}/api/invitationCode/my-codes`);
        return response.data;
    } catch (error) {
        console.error('Error fetching codes:', error);
        throw error;
    }
}

export const generateCodeService = async (time) => {
    try {
        const response = await axios.post(`${baseURL}/api/invitationCode/generate`, null, {
            params: {time: time},
        });
        return response.data;
    } catch (error) {
        console.error('Error generating code:', error);
        throw error;
    }
}

export const fetchUserInfoByIdService = async (userId) => {
    try {
        const response = await axios.get(`${baseURL}/api/user/info/${userId}`);
        return response.data;
    } catch (error) {
        console.error('Error fetching user info by ID:', error);
        throw error;
    }
}

export const fetchUploadedTorrentsService = async (page = 1, pageSize = 8) => {
    try {
        const response = await axios.get(`${baseURL}/torrent/upload/list`, {
            params: { page, pageSize },
        });
        return response.data;
    } catch (error) {
        console.error('Error fetching uploaded torrents:', error);
        throw error;
    }
};
export const updateUserProfileService = async (userData) => {
    try {
        const response = await axios.put(`${baseURL}/api/user/profile`, userData, {
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        });
        return response.data;
    } catch (error) {
        console.error('Error updating user profile:', error);
        throw error;
    }
}
